package com.tzbank.poc.util;

import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description:组规范报文
 * @Author: Create by li_zheng2
 * @Date: on 2019/1/21.
 */
public class ReturnMapUtil {

    public static void setHeaderMap(Map<String, Object> headerMap) {
        ReturnMapUtil.headerMap = headerMap;
    }

    public static Map<String,Object> returnErrorMap(String RetCode,String RetStatus,String RetMsg ) {
        setFailure();
        if(!StringUtils.isEmpty(RetCode)) {
            headerMap.put("RetCode", RetCode);
        }
        if(!StringUtils.isEmpty(RetStatus)) {
            headerMap.put("RetStatus", RetStatus);
        }
        if(!StringUtils.isEmpty(RetMsg)) {
            headerMap.put("RetMsg", RetMsg);
        }
        Map returnMap = new HashMap<>();
        serviceMap.put("Header",headerMap);
        serviceMap.put("Body",bodyMap);
        returnMap.put("Service", serviceMap);
        serviceMap.remove("Body");
        return returnMap;
    }



    static Map<String, Object> headerMap = new HashMap<>();

    public static void setBodyMap(Map<String, Object> bodyMap) {
        ReturnMapUtil.bodyMap = bodyMap;
    }

    static Map<String, Object> bodyMap = new HashMap<>();

    static Map<String, Object> serviceMap = new HashMap<>();


    public static Map<String, Object> getSuccessMap() {
        setHeaderMap();
        setSuccess();
        Map returnMap = new HashMap<>();
        serviceMap.put("Header",headerMap);
        serviceMap.put("Body",bodyMap);
        returnMap.put("Service", serviceMap);
        return returnMap;
    }

    public static Map<String, Object> getSuccessNoBody() {
        setHeaderMap();
        setSuccess();
        Map returnMap = new HashMap<>();
        serviceMap.put("Header",headerMap);
        serviceMap.put("Body",bodyMap);
        returnMap.put("Service", serviceMap);
        serviceMap.remove("Body");
        return returnMap;
    }

    public static Map<String, Object> getFailureMap() {
        Map returnMap = new HashMap<>();
        serviceMap.put("Header",headerMap);
        serviceMap.put("Body",bodyMap);
        returnMap.put("Service", serviceMap);
        return returnMap;
    }


    public static Map<String, Object> getBodyMap() {
        return bodyMap;
    }


    public static Map<String, Object> getHeaderMap() {
        return headerMap;
    }


    public static Map<String, String> getBody(Map map) {
        Map<String, Map> serviceMap = (Map<String, Map>) map.get("Service");
        Map<String, String> bodyMap = serviceMap.get("Body");
        return bodyMap;
    }

    public static Map<String, Object> returnFail(String errorMessage) {
        return ServiceUtil.returnFailure(errorMessage);
    }


    public static void setSuccess() {
        headerMap.put("RetStatus", "S");
        headerMap.put("RetCode", "0000");
        headerMap.put("RetMsg", "success");
    }

    private static void setFailure() {
        headerMap.put("RetStatus", "F");
        headerMap.put("RetCode", "1000");
        headerMap.put("RetMsg", "FAIL");
    }
    public static void setHeaderMap(){
        headerMap.put("BranchId", "801101");
        headerMap.put("SourceSysId", "");
        headerMap.put("ConsumerId", "test");
        headerMap.put("ServiceCode", "");
        headerMap.put("GlobalSeq", "");
        headerMap.put("TranSeq", "test");
        headerMap.put("Channel", "D07");
        headerMap.put("TranTeller", "100168");
        headerMap.put("TranDate", "20181126");
        headerMap.put("TranTime", "153829031");
        headerMap.put("RetMsg", "");
        headerMap.put("AuthTeller", "100168");
        headerMap.put("RetStatus", "");
        headerMap.put("RetCode", "");
        headerMap.put("LocalLang", "001");
        headerMap.put("LegalRepCode", "9999");
        headerMap.put("ServiceName", "111");
        headerMap.put("TranCode", "11");
    }

}
